package org.aceor.mddal.gateway.merger.function.aggregate;

import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.merger.function.AbstractAggregateFunction;
import org.aceor.mddal.gateway.merger.function.AggregateFunction;

/**
 * Created by lxue on 16/4/16.
 */
public class Count extends AbstractAggregateFunction implements AggregateFunction {
    private static final String COUNT = "COUNT";

    public Count(DataType returnDataType) {
        super(returnDataType);
    }

    @Override
    public Object getResult() {
        return value;
    }

    @Override
    public DataType getReturnType() {
        return returnDataType;
    }

    @Override
    public void merge(Object... values) {
        if (value == null) {
            value = values[0];
        } else {
            value = returnDataType.getCalculator().add(value, values[0]);
        }
    }

    @Override
    public String getFunctionName() {
        return COUNT;
    }
}
